{% extends "admin/layout.html" %}

{% block title %}项目管理 - {{ super() }}{% endblock %}

{% block content %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">项目管理</h1>
    <div class="btn-toolbar mb-2 mb-md-0">
        <a href="{{ url_for('jira.create_project') }}" class="btn btn-sm btn-outline-primary">
            <i class="fas fa-plus"></i> 新建项目
        </a>
    </div>
</div>

<div class="card shadow-sm mb-4">
    <div class="card-body">
        <form method="get" class="row g-3">
            <div class="col-md-3">
                <label for="status" class="form-label">状态筛选</label>
                <select class="form-select" id="status" name="status">
                    <option value="all" {% if status_filter == 'all' %}selected{% endif %}>全部状态</option>
                    <option value="draft" {% if status_filter == 'draft' %}selected{% endif %}>草稿</option>
                    <option value="pending_review" {% if status_filter == 'pending_review' %}selected{% endif %}>待审核</option>
                    <option value="approved" {% if status_filter == 'approved' %}selected{% endif %}>已批准</option>
                    <option value="rejected" {% if status_filter == 'rejected' %}selected{% endif %}>已拒绝</option>
                    <option value="in_progress" {% if status_filter == 'in_progress' %}selected{% endif %}>进行中</option>
                    <option value="completed" {% if status_filter == 'completed' %}selected{% endif %}>已完成</option>
                    <option value="closed" {% if status_filter == 'closed' %}selected{% endif %}>已关闭</option>
                </select>
            </div>
            <div class="col-md-3">
                <label for="category" class="form-label">类别筛选</label>
                <select class="form-select" id="category" name="category">
                    <option value="all" {% if category_filter == 'all' %}selected{% endif %}>全部类别</option>
                    {% for category in categories %}
                    <option value="{{ category.id }}" {% if category_filter == category.id|string %}selected{% endif %}>{{ category.name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-3 align-self-end">
                <button type="submit" class="btn btn-primary">筛选</button>
                <a href="{{ url_for('jira.project_list') }}" class="btn btn-secondary">重置</a>
            </div>
        </form>
    </div>
</div>

<div class="card shadow-sm">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>项目名称</th>
                        <th>类别</th>
                        <th>级别</th>
                        <th>负责人</th>
                        <th>状态</th>
                        <th>进度</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for project in projects.items %}
                    <tr>
                        <td>{{ project.name }}</td>
                        <td>{{ project.category.name if project.category }}</td>
                        <td>
                            <span class="badge" style="background-color: {{ project.level.color if project.level else '#6c757d' }}">
                                {{ project.level.name if project.level }}
                            </span>
                        </td>
                        <td>{{ project.owner.name }}</td>
                        <td>
                            <span class="badge bg-{{
                                'secondary' if project.status == 'draft'
                                else 'warning' if project.status == 'pending_review'
                                else 'success' if project.status == 'approved'
                                else 'danger' if project.status == 'rejected'
                                else 'primary' if project.status == 'in_progress'
                                else 'info' if project.status == 'completed'
                                else 'dark'
                            }}">
                                {{ project.status }}
                            </span>
                        </td>
                        <td>
                            <div class="progress" style="height: 8px;">
                                <div class="progress-bar" role="progressbar" style="width: {{ project.progress }}%;"
                                     aria-valuenow="{{ project.progress }}" aria-valuemin="0" aria-valuemax="100"></div>
                            </div>
                            <small class="text-muted">{{ project.progress }}%</small>
                        </td>
                        <td>{{ project.created_at.strftime('%Y-%m-%d') }}</td>
                        <td>
                            <div class="btn-group btn-group-sm">
                                <a href="{{ url_for('jira.project_detail', project_id=project.id) }}" class="btn btn-outline-info">详情</a>
                                {% if (project.status == 'draft' and project.owner_id == current_user.id) or current_user.is_admin %}
                                <a href="{{ url_for('jira.edit_project', project_id=project.id) }}" class="btn btn-outline-primary">编辑</a>
                                <form method="post" action="{{ url_for('jira.delete_project', project_id=project.id) }}" class="d-inline">
                                    <button type="submit" class="btn btn-outline-danger" onclick="return confirm('确定删除此项目吗？')">删除</button>
                                </form>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="8" class="text-center text-muted">暂无项目</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- 分页控件 -->
        {% if projects.pages > 1 %}
        <nav aria-label="Page navigation">
            <ul class="pagination justify-content-center">
                {% if projects.has_prev %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('jira.project_list', page=projects.prev_num, status=status_filter, category=category_filter) }}">上一页</a>
                </li>
                {% endif %}

                {% for page_num in projects.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=2) %}
                    {% if page_num %}
                        <li class="page-item {% if page_num == projects.page %}active{% endif %}">
                            <a class="page-link" href="{{ url_for('jira.project_list', page=page_num, status=status_filter, category=category_filter) }}">{{ page_num }}</a>
                        </li>
                    {% else %}
                        <li class="page-item disabled"><span class="page-link">...</span></li>
                    {% endif %}
                {% endfor %}

                {% if projects.has_next %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('jira.project_list', page=projects.next_num, status=status_filter, category=category_filter) }}">下一页</a>
                </li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}
    </div>
</div>
{% endblock %}